package kgame5.k3d.pars.myflint.zones {
import kgame5.k3d.core.math.Vector3D;
import kgame5.k3d.pars.myflint.geom.Vector3DFlint;
import kgame5.k3d.pars.myflint.geom.Vector3DUtilsFlint;
import kgame5.kmath.MathDG;

/**
 * @author kk
 */
public class MyDiscZone implements Zone3D{
		

		private var _center:Vector3DFlint;
		private var _normal:Vector3DFlint;
		private var _innerRadius:Number;
		private var _innerRadiusSq:Number;
		private var _outerRadius:Number;
		private var _outerRadiusSq:Number;
		private var _distToOrigin:Number;
		private var _planeAxis1:Vector3DFlint;
		private var _planeAxis2:Vector3DFlint;
		private var _dirty:Boolean;

		private static const TWOPI:Number = Math.PI * 2;
		
		
		private var _minAngle:Number;
		private var _maxAngle:Number;
		
		/**
		 * The constructor creates a DiscZone 3D zone.
		 * 
		 * @param centre The point at the center of the disc.
		 * @param normal A vector normal to the disc.
		 * @param outerRadius The outer radius of the disc.
		 * @param innerRadius The inner radius of the disc. This defines the hole 
		 * in the center of the disc. If set to zero, there is no hole. 
		 * dir表示的是在圆面上扇形的方向，
		 */
		public function MyDiscZone( center:Vector3DFlint, normal:Vector3DFlint, outerRadius:Number, innerRadius:Number = 0,dir:Vector3D=null,rangeAngle:int=360 )
		{
			_center = center.clone();
			_center.w = 1;
			_normal = normal.unit();
			_normal.w = 0;
			_innerRadius = innerRadius;
			_innerRadiusSq = _innerRadius * _innerRadius;
			_outerRadius = outerRadius;
			_outerRadiusSq = _outerRadius * _outerRadius;
			_dirty = true;
			
			//-----
			
			_minAngle=0;
			_maxAngle=TWOPI;
			
			if(dir!=null){
				var angle:Number=dir.getXOZAngle();
				_minAngle=(angle-rangeAngle/2)* MathDG.DTR;
				_maxAngle=(angle+rangeAngle/2)* MathDG.DTR;
			}
			
		}
		
		private function init():void
		{
			_distToOrigin = _normal.dotProduct( center );
			var axes:Array = Vector3DUtilsFlint.getPerpendiculars( normal );
			_planeAxis1 = axes[0];
			_planeAxis2 = axes[1];
			_dirty = false;
		}
		
		/**
		 * The point at the center of the disc.
		 */
		public function get center() : Vector3DFlint
		{
			return _center.clone();
		}
		public function set center( value : Vector3DFlint ) : void
		{
			_center = value.clone();
			_center.w = 1;
			_dirty = true;
		}

		/**
		 * The vector normal to the disc. When setting the vector, the vector is
		 * normalized. So, when reading the vector this will be a normalized version
		 * of the vector that is set.
		 */
		public function get normal() : Vector3DFlint
		{
			return _normal.clone();
		}
		public function set normal( value : Vector3DFlint ) : void
		{
			_normal = value.unit();
			_normal.w = 0;
			_dirty = true;
		}

		/**
		 * The inner radius of the disc.
		 */
		public function get innerRadius() : Number
		{
			return _innerRadius;
		}
		public function set innerRadius( value : Number ) : void
		{
			_innerRadius = value;
			_innerRadiusSq = _innerRadius * _innerRadius;
		}

		/**
		 * The outer radius of the disc.
		 */
		public function get outerRadius() : Number
		{
			return _outerRadius;
		}
		public function set outerRadius( value : Number ) : void
		{
			_outerRadius = value;
			_outerRadiusSq = _outerRadius * _outerRadius;
		}

		/**
		 * The contains method determines whether a point is inside the zone.
		 * This method is used by the initializers and actions that
		 * use the zone. Usually, it need not be called directly by the user.
		 * 
		 * @param p The location to test.
		 * @return true if the location is inside the zone, false if it is outside.
		 */
		public function contains( p:Vector3DFlint ):Boolean
		{
			if( _dirty )
			{
				init();
			}
			// is not in plane if dist to origin along normal is different
			var dist:Number = _normal.dotProduct( p );
			if( Math.abs( dist - _distToOrigin ) > 0.1 ) // test for close, not exact
			{
				return false;
			}
			// test distance to center
			var distToCenter:Number = Vector3DFlint.distanceSquared( center, p );
			if( distToCenter <= _outerRadiusSq && distToCenter >= _innerRadiusSq )
			{
				return true;
			}
			return false;
		}
		
		/**
		 * The getLocation method returns a random point inside the zone.
		 * This method is used by the initializers and actions that
		 * use the zone. Usually, it need not be called directly by the user.
		 * 
		 * @return a random point inside the zone.
		 */
		public function getLocation():Vector3DFlint
		{	//T.p("MyDistZone.getLocation");
			if( _dirty )
			{
				init();
			}
			var rand:Number = Math.random();
			var radius:Number = _innerRadius + (1 - rand * rand ) * ( _outerRadius - _innerRadius );
			
			//!!很恶心的东西，这个角度需要一个-90的校正，
			var angle:Number =-90* MathDG.DTR+_minAngle + Math.random() * ( _maxAngle - _minAngle );
			//T.p("!!",radius,angle,_minAngle,_maxAngle);
			return _center.add( _planeAxis1.multiply( radius * Math.cos( angle ) ).incrementBy( _planeAxis2.multiply( radius * Math.sin( angle ) ) ) );
			
			
			
//				//var rand:Number = Math.random();
//			var point:Point =  Point.polar(radius, _minAngle + Math.random() * ( _maxAngle - _minAngle ) );
//			var result:Vector3DFlint=_center.clone();
//			
//			result.x+=point.x;
//			result.z+=point.y;
//			
//			return result;
		}
		
		//
		public function getLocationByRad(radius:Number,rad:Number):Vector3DFlint{
			if( _dirty )
			{
				init();
			}
			//var rand:Number = Math.random();
			//var radius:Number = _innerRadius + (1 - rand * rand ) * ( _outerRadius - _innerRadius );
			
			//var angle:Number = rad;//Math.random() * TWOPI;
			//var rad:Number=angle* TWOPI;
			
			//这个需要校正一下才能用
			rad+=-90* MathDG.DTR;
			return _center.add( _planeAxis1.multiply( radius * Math.cos( rad ) ).incrementBy( _planeAxis2.multiply( radius * Math.sin( rad ) ) ) );
		}
		/**
		 * The getArea method returns the size of the zone.
		 * This method is used by the MultiZone class. Usually, 
		 * it need not be called directly by the user.
		 * 
		 * @return The surface area of the disc.
		 */
		public function getVolume():Number
		{
			// treat as one pixel tall disc
			return ( _outerRadius * _outerRadius - _innerRadius * _innerRadius ) * Math.PI;
		}
	}
}
